
Public Class specialKeysHandle

    Public Class specialKeys
        Public Const prefix As String = "___@$#_SPECIALKEY_#$@___"
        Public Shared ReadOnly prefixlen As Int32 = strlen(prefix)
        Public Const control As String = "^CTRL^"
        Public Shared ReadOnly controllen As Int32 = strlen(control)
        Public Const alt As String = "^ALT^"
        Public Shared ReadOnly altlen As Int32 = strlen(alt)
        Public Const shift As String = "^SHIFT^"
        Public Shared ReadOnly shiftlen As Int32 = strlen(shift)
    End Class

    Public Shared Function isTerminateKey(ByVal e As Windows.Forms.KeyEventArgs) As Boolean
        Return e.Control AndAlso e.KeyValue = Windows.Forms.Keys.Q
    End Function

    Public Shared Function isSpecialKey(ByVal e As Windows.Forms.KeyEventArgs) As Boolean
        Return isTerminateKey(e)
    End Function

    Public Shared Function packSpecialKey(ByVal e As Windows.Forms.KeyEventArgs) As String
        Dim rtn As String = specialKeys.prefix
        If e.Control Then
            rtn += specialKeys.control
        End If
        If e.Alt Then
            rtn += specialKeys.alt
        End If
        If e.Shift Then
            rtn += specialKeys.shift
        End If

        rtn += character.leftMidBracket + Convert.ToChar(e.KeyValue) + character.rightMidBracket
        Return rtn
    End Function

    Public Shared Function packSpecialKeyChar(ByVal s As String) As String
        assert(strlen(s) = 1, "not a special key char, call isSpecialKeyChar first")
        If isTerminateKeyChar(s(0)) Then
            Return packSpecialKey(New Windows.Forms.KeyEventArgs(Windows.Forms.Keys.Control Or Windows.Forms.Keys.Q))
        End If

        Return Nothing
    End Function

    Public Shared Function isTerminateKeyChar(ByVal c As Char) As Boolean
        Return Convert.ToInt32(c) = 17
    End Function

    Public Shared Function isSpecialKeyChar(ByVal s As String) As Boolean
        If strlen(s) <> 1 Then
            Return False
        Else
            Return isTerminateKeyChar(s(0))
        End If
    End Function

    Public Shared Function depackSpecialKey(ByVal input As String, _
                                ByRef e As Windows.Forms.KeyEventArgs) As Boolean
        On Error Resume Next

        e = Nothing
        kickNewlineChar(input)
        If strsame(input, 0, specialKeys.prefix, 0, specialKeys.prefixlen, True) Then
            Dim keyValue As Int32 = 0
            Dim control As Boolean = False
            Dim shift As Boolean = False
            Dim alt As Boolean = False
            Dim i As Int32
            For i = strlen(specialKeys.prefix) To strlen(input) - 1
                If strsame(input, i, specialKeys.control, 0, specialKeys.controllen, True) Then
                    control = True
                    i += strlen(specialKeys.control) - 1
                ElseIf strsame(input, i, specialKeys.shift, 0, specialKeys.shiftlen, True) Then
                    shift = True
                    i += strlen(specialKeys.shift) - 1
                ElseIf strsame(input, i, specialKeys.alt, 0, specialKeys.altlen, True) Then
                    alt = True
                    i += strlen(specialKeys.alt) - 1
                ElseIf strmid(input, i, 1) = character.leftMidBracket Then
                    Dim index As Int32 = input.IndexOf(character.rightMidBracket, i + 1)
                    If index <> npos AndAlso index - i - 1 = 1 Then
                        keyValue = Convert.ToInt32(strmid(input, i + 1, index - i - 1).Chars(0))
                        i = index
                    End If
                Else
                    Return False
                End If
            Next

            e = New Windows.Forms.KeyEventArgs(keyValue _
                                                Or ifoper(Of Int32)(control, Windows.Forms.Keys.Control, 0) _
                                                Or ifoper(Of Int32)(shift, Windows.Forms.Keys.Shift, 0) _
                                                Or ifoper(Of Int32)(alt, Windows.Forms.Keys.Alt, 0))
            Return True
        Else
            Return False
        End If
    End Function

End Class
